<?php
/**
 * 首页
 * @version 1.0.0 (Thu Feb 23 13:49:18 GMT 2012)
 * @author ZhangHao
 */

class Index extends CI_Controller {
	private $_data;

    public function __construct() {
		parent::__construct();
		$this->load->model('base_mdl', 'base');
    }

	public function index() {

		//分页配置
        $this->load->library('gpagination');
		
		$page = $this->input->get('page') > 1 ? $this->input->get('page') : '1';
		$this->_data['type'] = $type = $this->input->get('type');
		$limit = 30;
		$offset = ($page - 1) * $limit;
		$where  = array('state'=>1);
		$desc = 'ctime DESC';

		if($type) {
			if(intval($type) > 0) {
				$where['type'] = $type; 
			} else if($type == 'hot') {
				$desc = 'hits DESC, ctime DESC';
			} else if($type == 'top') {
				$where['top'] = 1; 
				$desc = 'toptime DESC';
			}
		}

		$total_num = $this->base->get_data('albums', $where)->num_rows();

		$this->gpagination->currentPage($page);
		$this->gpagination->items($total_num);
		$this->gpagination->limit($limit);
		$this->gpagination->target(site_url('index?type='.$type));

		$this->_data['pagination'] = $this->gpagination->getOutput();
		$this->_data['lists'] = $this->base->get_data('albums', $where, '*', $limit, $offset, $desc)->result_array();
		$this->_data['isMobile'] = $this->mobile->isMobile();

		$this->load->view(THEME.'/mobile_header', $this->_data);
		$this->load->view(THEME.'/index', $this->_data);
		$this->load->view(THEME.'/footer');
	}

	public function setTop() {
		if(!get_cookie('uid')) {
			exit('0');
		}

		$aid = intval($_GET['aid']);

		$this->base->update_data('albums', array('id'=>$aid), array('top'=>1, 'toptime'=>date('Y-m-d H:i:s')));
		echo 1;				
	}

	public function search() {

		//分页配置
        $this->load->library('gpagination');
        $this->_data['keyword'] = $keyword = $this->input->get('keyword');
		$page = intval($this->input->get('page')) ? intval($this->input->get('page')) : 1;
		$limit = 30;
		$offset = ($page-1)*$limit;

		$total_num = $this->db->query("SELECT * FROM mt_albums WHERE state=1 AND title LIKE '%".$keyword."%' OR keyword LIKE '%".$keyword."%'")->num_rows();
		$this->gpagination->currentPage($page);
		$this->gpagination->items($total_num);
		$this->gpagination->limit($limit);
		$this->gpagination->target(site_url('index/search?keyword='.$keyword));

		$this->_data['pagination'] = $this->gpagination->getOutput();

		$this->_data['lists'] = $this->db->query("SELECT * FROM mt_albums WHERE state=1 AND title LIKE '%".$keyword."%' OR keyword LIKE '%".$keyword."%' LIMIT ".$offset.", ".$limit)->result_array();

		$this->load->view(THEME.'/header', $this->_data);
		$this->load->view(THEME.'/index', $this->_data);
		$this->load->view(THEME.'/footer');			
	}

	public function android() {
		$this->load->helper('download');
		$data = file_get_contents("data/youhuode-0.0.7.apk"); // 读文件内容
		$name = 'youhuode-0.0.7.apk';

		force_download($name, $data); 
	}

}